using System.Collections;
using System.IO;
using SpellCore;

namespace SpellCenter.Core
{
    static public class WritingSystemDictionary
    {
        static ISpellChecker GetCheckerFromId(string writingSystemId)
        {
            ISpellChecker spellChecker = Speller.SpellChecker[writingSystemId];
            if(!spellChecker.HasEngines)
            {
                spellChecker.CreateDefaultEngine();
            }
          return spellChecker;
        }

        static public CheckingResult GetStatusOfWord(string writingSystemId, string word)
        {
          return GetCheckerFromId(writingSystemId).GetWordStatus(word);
        }

      static public void AddGoodWord(string writingSystemId, string word)
        {
          GetCheckerFromId(writingSystemId).AddGoodWord(word);            
        }

      public static void AddBadWord(string writingSystemId, string word)
        {
          GetCheckerFromId(writingSystemId).AddBadWord(word);
        }


      public static IEnumerable GetSuggestionsForWord(string writingSystemId, string word)
        {
          return GetCheckerFromId(writingSystemId).GetSuggestions(word);
        }

        /// <summary>
        /// Bootstrap the dictionary from a custom word list.  This file should contain
        /// a list of words separated by newlines.
        /// </summary>
        /// <param name="writingSystemId">writing sytem identifier (e.g. en-US)</param>
        /// <param name="file">name of file</param>
      public static void AddGoodWordsFromFile(string writingSystemId, string file)
        {
          ISpellChecker checker = GetCheckerFromId(writingSystemId);
          using (StreamReader text = File.OpenText(file))
            {
                while (!text.EndOfStream)
                {
                    checker.AddGoodWord(text.ReadLine());
                }
            }
        }

        /// <summary>
        /// Bootstrap the dictionary from a custom word list.  This file should contain
        /// a list of words separated by newlines. These words will be automatically marked
        /// as bad words.
        /// </summary>
        /// <param name="writingSystemId">writing sytem identifier (e.g. en-US)</param>
        /// <param name="file">name of file</param>
      public static void AddBadWordsFromFile(string writingSystemId, string file)
        {
          ISpellChecker checker = GetCheckerFromId(writingSystemId);
          using (StreamReader text = File.OpenText(file))
            {
                while (!text.EndOfStream)
                {
                    checker.AddBadWord(text.ReadLine());
                }
            }
        }

      static public bool IsSpelledCorrect(string writingSystemId, string word)
        {
            return Speller.IsCorrect(writingSystemId, word);
        }
    }
}